/**
 * Created by yyj on 2015/10/28.
 */
$(function(){
	//点击提交按钮
	$('.press_btn1.Sub').click(function(){
		
      var index = parent.layer.getFrameIndex(window.name);
        var state=$("#state").val().trim();
        var remark=$("#remark").val().trim();
        var depName=$("#depName").val().trim();
        var cmpName=$("#cmpName").val().trim();
        var isoutdep= $("#isOutDep").val().trim();
        var depId=$("#depId").val().trim();
    	if(isoutdep=='否'){
    	var parName=$("#nothing").val();
    	}else{
    	var	parName=$("#parentDep").val();
    	}
        if(depName==""||state==""||parName==""||parName==null){
        	parent.layer.msg('带*号的不能为空！', {time:1300});
        }else{
        	$.ajax({
            url:"editSysDep.do",
            type:"post",
            dataType: 'json',
            data:{
        		"state":state,
        		"remark":remark,
        		"depName":depName,
        		"cmpName":cmpName,
        		"parName":parName,
        		"depId":depId
            },
            success:function(result){
                if(result.data){
                   	parent.paging(1);
                   	parent.$('.checkAll').removeAttr('checked');
                    parent.layer.msg(result.errorMessage, {time:1300});
       			 	parent.layer.close(index);
                }else{
                    parent.layer.msg(result.errorMessage, {time:1000});
                }
            }
        });
        }
    })

    //点击关闭按钮
    $('.press_btn2.Close').click(function(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.$('.checkAll').removeAttr('checked');
        parent.layer.close(index);
    })
    //点击添加按钮
     $('.press_btn1.Keep').click(function(){
        var index = parent.layer.getFrameIndex(window.name);
        var state=$("#state").val().trim();
        var remark=$("#remark").val().trim();
        var depName=$("#depName").val().trim();
        var cmpName=$("#cmpName").val().trim();
        var isoutdep= $("#isOutDep").val().trim();
    	if(isoutdep=='否'){
    	var parName=$("#nothing").val();
    	}else{
    	var	parName=$("#parentDep").val();
    	}
        if(depName==""||state==""||parName==""||parName==null){
        	parent.layer.msg('带*号的不能为空！', {time:1300});
        }else{
        	$.ajax({
            url:"addSysDep.do",
            type:"post",
            dataType: 'json',
            data:{
        		"state":state,
        		"remark":remark,
        		"depName":depName,
        		"cmpName":cmpName,
        		"parName":parName
            },
            success:function(result){
                if(result.data){
                   	parent.paging(1);
                   	parent.$('.checkAll').removeAttr('checked');
                    parent.layer.msg(result.errorMessage, {time:1300});
       			 	parent.layer.close(index);
                }else{
                    parent.layer.msg(result.errorMessage, {time:1000});
                }
            }
        });
        }
    })
    
    $("#isOutDep").change(function(){
    	var isoutdep= $("#isOutDep").val();
    	if(isoutdep=='否'){
    		$("#isOutDep option").each(function(i,o){ 
				if($(this).attr("selected")) { 
					$(".outDep").hide(); 
					$(".outDep").eq(i).show(); 
				} 
			}); 
    	}else{
    		$("#isOutDep option").each(function(i,o){ 
				if($(this).attr("selected")) { 
					$(".outDep").hide(); 
					$(".outDep").eq(i).show(); 
				} 
			}); 
			if($("#depId").val() == "" && $("#depId").val() == null){
				changeDep();
			}else{
				changeEditDep();
			}
    	}
    })
    
    $("#isOutDep").change();
    
    $("#cmpName").change(function(){
    	if($("#depId").val() == "" && $("#depId").val() == null){
				changeDep();
		}else{
			changeEditDep();
		}
    });
});

function changeDep(){
	var cmpId = $(":selected").attr("cmpId");
	$.ajax({
            url:"selectDepBycmpId.do",
            type:"post",
            dataType: 'json',
            data:{
            	"cmpId":cmpId
            },
            success:function(result){
            	var depList = result.data;
            	if(depList != null){
            		 document.getElementById("parentDep").options.length=0;
            		for(var i = 0; i<depList.length;i++){
            			document.getElementById("parentDep").options.add(new Option(depList[i].depName, depList[i].depName));
            		}
            	}
            }
      });
}

function changeEditDep(){
	var cmpId = $(":selected").attr("cmpId");
	var depId = $("#depId").val();
	$.ajax({
            url:"selectEditDepBycmpId.do",
            type:"post",
            dataType: 'json',
            data:{
            	"cmpId":cmpId,
            	"depId":depId
            },
            success:function(result){
            	var depList = result.data;
            	if(depList != null){
            		 document.getElementById("parentDep").options.length=0;
            		for(var i = 0; i<depList.length;i++){
            			document.getElementById("parentDep").options.add(new Option(depList[i].depName, depList[i].depName));
            			var depPid = $("#depPid").val();
	            		if(depPid == depList[i].depId){
	            			
	            		}
            		}
            		var sel = document.getElementById("parentDep");
            		for(var i=0;i<sel.length;i++){
            			if(depPid == depList[i].depId){
	            			sel[i].selected = true;
	            		}
            		}
            	}
            }
      });
}
